package com.jlkf.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jlkf.pojo.TblCourse;
import com.jlkf.pojo.TblGuidePage;
import com.jlkf.pojo.TblOtherCourse;
import com.jlkf.pojo.TblUpload;
import com.jlkf.service.OtherService;
import com.platform.server.response.response.pojo.ReturnData;
import com.platform.server.util.StringTools;

@Controller
@RequestMapping("other")
public class OtherController {
	@Autowired
	private OtherService otherService;
	
	/**
	 * 线下课程以及直播列表
	 * @param type
	 * @param name
	 * @param pageNumber
	 * @param pageSize
	 * @return
	 */
	@RequestMapping("getListByType")
	@ResponseBody
	public ReturnData selectList(Integer type,String name,String teacher,Integer pageNumber,Integer pageSize){
		Map<String, Object> map = new HashMap<String,Object>();
		if(!StringTools.isEmpty(name)){
			map.put("name", name);
		}
		if(!StringTools.isEmpty(teacher)){
			map.put("teacher", teacher);
		}
		if(type!=null){
			map.put("type", type);
		}
		PageHelper.startPage(pageNumber, pageSize);
		List<TblOtherCourse> list = otherService.getListByType(map);
		PageInfo<TblOtherCourse> pageInfo = new PageInfo<TblOtherCourse>(list);
		return new ReturnData(list,pageInfo.getPages(),(int)pageInfo.getTotal(),200,"线下课程以及直播列表");
	}
	
	/**
	 * 查看详情
	 */
	@RequestMapping("getDetail")
	@ResponseBody
	public ModelAndView getDetail(Integer gId,Integer type){
		TblOtherCourse course = otherService.getDetail(gId);
		ModelAndView andView = new ModelAndView();
    	andView.addObject("course",course);
    	if(type==1){//线下课程
    		andView.setViewName("jsp/otherManager/addOfflineCourse");
    	}else{ //直播列表
    		andView.setViewName("jsp/otherManager/addOrUpdateLive");
    	}
		return andView;
	}
	
	
	/**
	 * 新增或编辑
	 * @param json
	 * @return
	 */
	@RequestMapping("addOrUpdate")
	@ResponseBody
	public ReturnData addOrUpdate(@RequestBody String json){
		try{
			TblOtherCourse course= StringTools.json2Obj(json, TblOtherCourse.class);
			if(course.getgId()!=null){
				int ret = otherService.update(course);
				if(ret>0){
					return new ReturnData(200,"修改成功");
				}else{
					return new ReturnData(500,"修改失败");
				}
			}else{
				course.setgCreateTime(new Date());
				int ret = otherService.insert(course);
				if(ret>0){
					return new ReturnData(200,"添加成功");
				}else{
					return new ReturnData(500,"添加失败");
				}
			}
		}catch (Exception e) {
			e.printStackTrace();
			return new ReturnData(500,"参数传入有误");
		}
	}
	
	/**
	 * 删除
	 * @param gId
	 * @return
	 */
	@RequestMapping("delete")
	@ResponseBody
	public ReturnData delete(Integer gId){
		int rel = otherService.delete(gId);
		if(rel>0){
			return new ReturnData(200,"删除成功");
		}else{
			return new ReturnData(500,"删除失败");
		}
	}
	
	/**
	 * 资源列表
	 * @param type
	 * @param name
	 * @param pageNumber
	 * @param pageSize
	 * @return
	 */
	@RequestMapping("getList")
	@ResponseBody
	public ReturnData getList(Integer pageNumber,Integer pageSize){
		PageHelper.startPage(pageNumber, pageSize);
		List<TblUpload> list = otherService.getList();
		PageInfo<TblUpload> pageInfo = new PageInfo<TblUpload>(list);
		return new ReturnData(list,pageInfo.getPages(),(int)pageInfo.getTotal(),200,"资源列表列表");
	}

	/**
	 * 新增或编辑
	 * @param json
	 * @return
	 */
	@RequestMapping("addUpload")
	@ResponseBody
	public ReturnData addUpload(@RequestBody String json){
		try{
			TblUpload upload = StringTools.json2Obj(json, TblUpload.class);
			int rel = otherService.insertUpload(upload);
			if(rel>0){
				return new ReturnData(200,"添加成功");
			}else{
				return new ReturnData(500,"添加失败");
			}
		}catch (Exception e) {
			e.printStackTrace();
			return new ReturnData(500,"参数传入有误");
		}
	}
	
	/**
	 * 删除
	 * @param gId
	 * @return
	 */
	@RequestMapping("deleteUpload")
	@ResponseBody
	public ReturnData deleteUpload(Integer id){
		int rel = otherService.deleteUpload(id);
		if(rel>0){
			return new ReturnData(200,"删除成功");
		}else{
			return new ReturnData(500,"删除失败");
		}
	}
}
